package com.tccmeta.httpApi.intercepter;

import lombok.extern.java.Log;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.script.ScriptException;

/**
 * @author pan
 * WEB API全局异常处理器
 */
@ControllerAdvice
@Log
public class MyExceptionHandler {

    @ExceptionHandler(value =Exception.class)
    public ResponseEntity exceptionHandler(Exception e){
        log.info("服务错误："+e.getMessage());
        e.printStackTrace();
        ResponseEntity result = new ResponseEntity(e.getMessage(),HttpStatus.INTERNAL_SERVER_ERROR);
        return result;
    }
    @ExceptionHandler(value = ScriptException.class)
    public ResponseEntity exceptionHandler(ScriptException e){
        ResponseEntity result = new ResponseEntity(e.getMessage(),HttpStatus.INTERNAL_SERVER_ERROR);
        if(e.getCause().getCause() instanceof RuntimeException){
            log.info("运行时异常："+e.getMessage());
            result = new ResponseEntity(e.getMessage(),HttpStatus.BAD_REQUEST);
        }
        return result;
    }
}